package com.qeeyou.apps.android.craftsman.activity.publish;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.qeeyou.apps.android.craftsman.R;
import com.qeeyou.apps.android.craftsman.po.InstallMaterialInfo;
import com.qeeyou.apps.android.craftsman.po.InstallMaterialPriceInfo;
import com.qeeyou.apps.android.craftsman.po.UserAddressInfo;
import com.qeeyou.apps.android.craftsman.po_wrap.InstallMaterialWrapInfo;
import com.qeeyou.apps.android.craftsman.service.AppService;
import com.qeeyou.apps.android.craftsman.service.UserService;
import com.qeeyou.apps.android.craftsman.view.CustomLoadingDialog;
import com.qeeyou.apps.android.craftsman.view.wheelview.WheelView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InstallMaterialActivity extends Activity {

    private Button btnCancel;                               //取消按钮
    private Button btnConfirm;                              //确定按钮
    private WheelView wheelView;
    private CustomLoadingDialog mLoadingDialog;
    private List<InstallMaterialWrapInfo> materialWrapList; //材料列表
    private InstallMaterialWrapInfo materialInfo;           //安装材质

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intall_material);

        getWindow().setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

        setView();
        setEvent();
    }

    private void setView() {
        wheelView = (WheelView) this.findViewById(R.id.wheelView4InstallMaterial);
        btnCancel = (Button) this.findViewById(R.id.btnCancel4InstallMaterial);
        btnConfirm = (Button) this.findViewById(R.id.btnConfirm4InstallMaterial);
    }

    private void setEvent() {
        if(materialWrapList == null || materialWrapList.size() == 0) {
            if(materialWrapList == null) {
                materialWrapList = new ArrayList<InstallMaterialWrapInfo>();
            }
            //填充
            List<String> itemList = new ArrayList<String>();
            itemList.add("不锈钢");
            itemList.add("木质");
            wheelView.setItems(itemList, 0);
            new GetInstallMaterialList().execute(new String[] {});
        } else {
            List<String> itemList = new ArrayList<String>();
            for(int i = 0, n = materialWrapList.size(); i < n; i++) {
                InstallMaterialInfo materialInfo = materialWrapList.get(i).getMaterialInfo();
                itemList.add(materialInfo.getMaterialname());
            }
            wheelView.setItems(itemList, 0);
            wheelView.setOnItemSelectedListener(onItemSelectedListener);
        }
        btnCancel.setOnClickListener(onCancelClickListener);
        btnConfirm.setOnClickListener(onConfirmClickListener);
    }


    //返回按钮
    private View.OnClickListener onCancelClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    InstallMaterialActivity.this.finish();
                }
            }, 500);
        }
    };

    //确定按钮
    private View.OnClickListener onConfirmClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(materialInfo != null) {
                Intent intent = new Intent();
                intent.putExtra("install_material", materialInfo);
                InstallMaterialActivity.this.setResult(RESULT_OK, intent);
            }
            InstallMaterialActivity.this.finish();
        }
    };


    private WheelView.OnItemSelectedListener onItemSelectedListener = new WheelView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(int selectedIndex, String item) {
            if(materialWrapList != null) {
                materialInfo = materialWrapList.get(selectedIndex);
            } else {
                materialInfo = null;
            }
        }
    };

    class GetInstallMaterialList extends AsyncTask<String, Void, List<InstallMaterialWrapInfo>> {

        public GetInstallMaterialList() {
            super();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if (mLoadingDialog == null) {
                mLoadingDialog = new CustomLoadingDialog(InstallMaterialActivity.this);
            }
            mLoadingDialog.setMessage("正在获取安装材料列表...");
            mLoadingDialog.show();
        }

        @Override
        protected List<InstallMaterialWrapInfo> doInBackground(String... params) {
            //获取价格列表
            Map<String, String> priceParamMap = new HashMap<String, String>();
            List<InstallMaterialPriceInfo> priceList = AppService.getInstance().getInstallMaterialPriceList(priceParamMap);
            //获取材料列表
            Map<String, String> paramMap = new HashMap<String, String>();
            List<InstallMaterialInfo> materialList = AppService.getInstance().getInstallMaterialList(paramMap);

            List<InstallMaterialWrapInfo> wrapInfoList = new ArrayList<InstallMaterialWrapInfo>();
            if(materialList != null && materialList.size() > 0) {
                for(int i = 0, n = materialList.size(); i < n; i++) {
                    InstallMaterialInfo materialInfo = materialList.get(i);
                    String materialId = materialInfo.getId();
                    InstallMaterialPriceInfo priceInfo = getPriceInfo(materialId, priceList);
                    InstallMaterialWrapInfo wrapInfo = new InstallMaterialWrapInfo();
                    wrapInfo.setMaterialInfo(materialInfo);
                    wrapInfo.setPriceInfo(priceInfo);
                    wrapInfoList.add(wrapInfo);
                }
            }
            return wrapInfoList;
        }

        //根据材料获取相应的价格信息
        private InstallMaterialPriceInfo getPriceInfo(String materialId, List<InstallMaterialPriceInfo> priceInfoList) {
            if(materialId == null || materialId.trim().equalsIgnoreCase("")) {
                return null;
            }
            if(priceInfoList == null || priceInfoList.isEmpty()) {
                return null;
            }
            int selectIndex = -1;
            for(int i = 0, n = priceInfoList.size(); i < n; i++) {
                InstallMaterialPriceInfo priceInfo = priceInfoList.get(i);
                if(priceInfo.getMaterialid().trim().equalsIgnoreCase(materialId)) {
                    selectIndex = i;
                    break;
                }
            }
            if(selectIndex >= 0) {
                InstallMaterialPriceInfo selectPriceInfo = priceInfoList.get(selectIndex);
                return selectPriceInfo;
            } else {
                return null;
            }
        }

        @Override
        protected void onPostExecute(List<InstallMaterialWrapInfo> materialWrapList) {
            super.onPostExecute(materialWrapList);
            if (mLoadingDialog != null) {
                mLoadingDialog.dismiss();
                mLoadingDialog = null;
            }
            if(materialWrapList != null && !materialWrapList.isEmpty()) {
                InstallMaterialActivity.this.materialWrapList.clear();
                InstallMaterialActivity.this.materialWrapList.addAll(materialWrapList);
                List<String> titleList = new ArrayList<String>();
                for(int i = 0, n = materialWrapList.size(); i < n; i++) {
                    titleList.add(materialWrapList.get(i).getMaterialInfo().getMaterialname());
                }
                wheelView.setItems(titleList, 0);
                materialInfo = InstallMaterialActivity.this.materialWrapList.get(0);

                wheelView.setIsLoop(false);
                wheelView.setOnItemSelectedListener(onItemSelectedListener);
            }

        }
    }


}
